package com.wangyk.rabbitmqcommon.fdfs;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.OSSObject;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;

/**
 * @author yangSir
 * @version 1.0.0
 * @ClassName OSSClient
 * @Description OSS客户端
 * @Date Oct 26, 2016 10:55:49 AM
 */
public class OssClient implements IFileClient
{
    // endpoint以杭州为例，其它region请按实际情况填写
    // private String endpoint;

    // private String vpcendpoint;

    // private String secretAccessKey;

    // yourBucketName
    private String bucketName;

    private OSSClient ossClient = null;

    public OssClient()
    {
    }

    public OssClient(String endpoint, String accessKeyId, String secretAccessKey, String bucketName)
    {
        this.bucketName = bucketName;
        // this.endpoint = endpoint;
        String vpcendpoint = "vpc100-" + endpoint;
        // this.secretAccessKey = Base64.decodeStr(secretAccessKey);
        // this.accessKeyId = Base64.decodeStr(accessKeyId);
        // 创建OSSClient实例
        // 数据解密
        ossClient = new OSSClient(vpcendpoint, Base64.decodeStr(accessKeyId), Base64.decodeStr(secretAccessKey));
    }

    public String uploadFile(byte[] fileBuff, String fileName)
    {
        String key = null;
        try
        {
            String rightName = fileName.substring(0, fileName.lastIndexOf("."));
            String extName = fileName.substring(fileName.lastIndexOf(".") + 1);
            // yourKey
            key = rightName + "_" + System.nanoTime() + "." + extName;
            ossClient.putObject(bucketName, key, new ByteArrayInputStream(fileBuff));
        }
        catch (Exception e)
        {
            throw new FastDFSException();
        }

        return key;
    }

    public String uploadFile(String localFileName, String fileName)
    {
        String key = null;
        try
        {

            String rightName = fileName.substring(0, fileName.lastIndexOf("."));
            String extName = fileName.substring(fileName.lastIndexOf(".") + 1);
            // yourKey
            key = rightName + "_" + System.nanoTime() + "." + extName;
            ossClient.putObject(bucketName, key, new File(localFileName));

        }
        catch (Exception e)
        {
            throw new FastDFSException();
        }

        return key;
    }

    public int deleteFile(String url)
    {
        int result = 0;
        try
        {
            if (null == null)
            {
                return 0;
            }
            // 删除Object ossClient.deleteObject("<bucketName>", "<key>");
            ossClient.deleteObject(bucketName, url);
        }
        catch (Exception e)
        {
        }

        return result;
    }

    public void clientShutdown()
    {
        ossClient.shutdown();
    }

    // @Override
    // public String getFilePrefectUrl(String partOfUrl)
    // {
    // try
    // {
    // // String expires = "" + (System.currentTimeMillis() / 1000 + 60);
    // StringBuffer sb = new StringBuffer();
    // // sb.append("GET\n\n\n");
    // // sb.append(expires);
    // // sb.append("\n\n/");
    // // sb.append(bucketName);
    // // sb.append("/");
    // // sb.append(partOfUrl);
    // // // String test = "GET\n\n\1477648216\n/yizhi-bucket/1477548071763test.png";
    // // //System.out.println(sb.toString());
    // // //System.out.println(secretAccessKey);
    // // byte[] hmacsha1 = HMACSHA1.HmacSHA1Encrypt(secretAccessKey, sb.toString());
    // // String signature = URLEncoder.encode(Base64.encode(hmacsha1));
    //
    // sb = new StringBuffer();
    // sb.append("http://");
    // sb.append(bucketName);
    // sb.append(".");
    // sb.append(endpoint);
    // sb.append("/");
    // sb.append(partOfUrl);
    // String yuming = sb.toString();
    // return yuming;
    // // sb = new StringBuffer();
    // // sb.append(yuming);
    // // sb.append("?OSSAccessKeyId=");
    // // sb.append(accessKeyId);
    // //
    // // sb.append("&Expires=");
    // // sb.append(expires);
    // // sb.append("&Signature=");
    // // sb.append(signature);
    //
    // //
    // http://oss-example.oss-cn-hangzhou.aliyuncs.com/oss-api.pdf?OSSAccessKeyId=44CF9590006BF252F707&Expires=1141889120&Signature=vjbyPxybdZaNmGa%2ByT272YEAiv4%3D
    // // return sb.toString();
    //
    // }
    // catch (Exception e)
    // {
    // logger.error(e.getMessage(), e);
    // throw new XuefuException(ExceptionCode.FDFS_UPLOAD_FILE_CODE, e);
    //
    // }
    // }

    public InputStream getDownloadInput(String filePath)
    {
        try
        {
            // OSSObject ossObject = ossClient.getObject("bucketName", "yourKey");
            // 数据库中存储的数据
            OSSObject ossObject = ossClient.getObject(bucketName, filePath);
            InputStream in = ossObject.getObjectContent();
            return in;
        }
        catch (Exception e)
        {
            throw new FastDFSException();
        }

    }

}
